﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace aPCSuite
{
    public partial class frmContact : Form
    {
        private SMSFramework _SMSFramework = new SMSFramework();
        public delegate void ChooseContact(object sender, EventArgs e);
        public event ChooseContact _contact_callback = null;
        private List<string> number_select = new List<string>();
        private int start = 0;
        private int limit = 20;
        public frmContact()
        {
            InitializeComponent();
            _SMSFramework.parent = this;
            //GetContactList();
           
            // Set the border style to a single, flat border.
            /*listBox1.BorderStyle = BorderStyle.FixedSingle;
            listBox1.DrawMode = DrawMode.OwnerDrawVariable;
            listBox1.MeasureItem +=
                new MeasureItemEventHandler(ListBox1_MeasureItem);
            listBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);/**/
            //listBox1.Font = new Font(listBox1.Font.FontFamily, 12);

            btnNext.Enabled = false;
            btnPrevious.Enabled = false;

            /*List<string> _items = new List<string>();
            _items.Add("One <test>");
            _items.Add("two<test1>");
            _items.Add("three<test2>");
            listBox1.DataSource = _items;/**/
            //listBox1.ItemHeight = 20;

        }

        public void GetContactList()
        {
            string req = XmlRequest();
            LogFile.WriteAppendLog("GetContactList : " + req, LogFile._LogFile);
            _SMSFramework.srequest = req;
            _SMSFramework._callback += new callback(_callback);
            _SMSFramework.Start();/**/
        }

        private string XmlRequest()
        {
            string req = "<protocol encoding=\"UTF-8\" version=\"1.0\">";
            req += "<app_id>1</app_id>";
            req += "<session_id>" + Transaction._session_id + "</session_id>";
            req += "<sms_contact>";
            req += "<dev_code>" + aPCSuite.Properties.Settings.Default.dev_code + "</dev_code>";
            req += "<limit>" + start.ToString() + "," + limit.ToString() + "</limit>";
            req += "</sms_contact>";
            req += "</protocol>";
            return req;
        }

        private void _callback(object sender, EventArgs e)
        {
            try
            {
                protocol ret = sender as protocol;
                if (ret != null && ret._contact_list != null)
                {
                    this.Invoke(new MethodInvoker(delegate
                    {
                        listBox1.DataSource = null;
                        /*List<string> _items = new List<string>();
                        for (int i = 0; i < ret._contact_list.arr_contact.Length; ++i)
                        {
                            string item = ret._contact_list.arr_contact[i].number;
                            item += "<";
                            item += ret._contact_list.arr_contact[i].name;
                            item += ">";
                            _items.Add(item);
                        }
                        listBox1.DataSource = _items;
                        /**/
                        frmMessaging._contact_list.Clear();
                        for (int i = 0; i < ret._contact_list.arr_contact.Length; ++i)
                        {
                            string item = ret._contact_list.arr_contact[i].number;
                            item += "<";
                            item += ret._contact_list.arr_contact[i].name;
                            item += ">";
                            frmMessaging._contact_list.Add(item);
                        }
                        listBox1.DataSource = frmMessaging._contact_list;

                        // Enable next, previous
                        if ((start + limit) < ret._contact_list.count)
                            btnNext.Enabled = true;
                        else
                            btnNext.Enabled = false;

                        //if ((start - limit) > 0)
                        if(start > 0)
                            btnPrevious.Enabled = true;
                        else
                            btnPrevious.Enabled = false;
                    }));
                }
                else
                {
                    //this.Invoke(new MethodInvoker(delegate { this.Close(); }));
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("frmContact _callback : {0}", ex.Message);
            }/**/
        }

        // Handle the DrawItem event for an owner-drawn ListBox.
        private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
        {

            // If the item is the selected item, then draw the rectangle
            // filled in blue. The item is selected when a bitwise And  
            // of the State property and the DrawItemState.Selected 
            // property is true.
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                e.Graphics.FillRectangle(Brushes.CornflowerBlue, e.Bounds);
            }
            else
            {
                // Otherwise, draw the rectangle filled in beige.
                e.Graphics.FillRectangle(Brushes.Beige, e.Bounds);
            }

            // Draw a rectangle in blue around each item.
            e.Graphics.DrawRectangle(Pens.DarkGray, e.Bounds);

            // Draw the text in the item.
            if(listBox1.Items.Count > 0)
                e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),
                    this.Font, Brushes.Black, e.Bounds.X, e.Bounds.Y);

            // Draw the focus rectangle around the selected item.
            e.DrawFocusRectangle();
        }

        // Handle the MeasureItem event for an owner-drawn ListBox.
        private void ListBox1_MeasureItem(object sender,
            MeasureItemEventArgs e)
        {

            // Cast the sender object back to ListBox type.
            ListBox theListBox = (ListBox)sender;

            // Get the string contained in each item.
            string itemString = (string)theListBox.Items[e.Index];

            // Split the string at the " . "  character.
            string[] resultStrings = itemString.Split('.');

            // If the string contains more than one period, increase the 
            // height by ten pixels; otherwise, increase the height by 
            // five pixels.
            /*if (resultStrings.Length > 2)
            {
                e.ItemHeight += 10;
            }
            else
            {
                e.ItemHeight += 5;
            }/**/
            e.ItemHeight += 15;

        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (_contact_callback == null)
                return;

            string contact_number = listBox1.SelectedItem as string;
            if (!number_select.Contains(contact_number))
            {
                //_contact_callback(listBox1.SelectedItem, EventArgs.Empty);
                number_select.Add(contact_number);
            }
            else
            {
                number_select.Remove(contact_number);
            }
            _contact_callback(number_select, EventArgs.Empty);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (_contact_callback == null)
                return;

            //string contact_number = "";
            for (int i = 0; i < listBox1.SelectedItems.Count; ++i)
            {
                string _selected = listBox1.SelectedItems[i] as string;
                if (!number_select.Contains(_selected))
                {
                    //contact_number += _selected;
                    //contact_number += "\n";
                    number_select.Add(_selected);
                }
            }
            /*if (contact_number.Length > 0)
            {
                contact_number = contact_number.Substring(0, contact_number.Length - 1);
                _contact_callback(contact_number, EventArgs.Empty);
            }/**/
            _contact_callback(number_select, EventArgs.Empty);
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            start = start - limit;
            GetContactList();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            start = start + limit;
            GetContactList();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            GetContactList();
        }

        public void SetDataSource()
        {
            listBox1.DataSource = null;
            listBox1.DataSource = frmMessaging._contact_list;
        }
    }
}
